package service

import (
	"context"
	"github.com/shopspring/decimal"
	"github.com/spf13/cast"
	"insighttracking/common/utils"
	"insighttracking/query/bo"
	"insighttracking/query/dao"
	"insighttracking/query/model"
	"time"
)

type _purchaseStoreService struct {
}

func (s _purchaseStoreService) CreatePurchaseStoreApi(ctx context.Context, tx *dao.QueryTx, store *model.PurchaseStoreEntity) (err error) {
	if err = tx.PurchaseStoreEntity.WithContext(ctx).Omit(
		dao.PurchaseStoreEntity.OperateDate,
		dao.PurchaseStoreEntity.OperateID,
		dao.PurchaseStoreEntity.OperateName,
	).Create(store); err != nil {
		return err
	}
	return nil
}

func (s _purchaseStoreService) GetPurchaseStorePageApi(ctx context.Context, req bo.GetPurchaseStorePageBoReq) ([]bo.GetPurchaseStorePageBoResp, int64, error) {
	tx := dao.PurchaseStoreEntity.WithContext(ctx).WithContext(ctx).Where(dao.PurchaseStoreEntity.EnterpriseID.Eq(req.EnterpriseID))
	if req.OrderNo != "" {
		tx = tx.Where(dao.PurchaseStoreEntity.OrderNo.Eq(req.OrderNo))
	}
	if req.StartDate != "" && req.EndDate != "" {
		tx = tx.Where(dao.PurchaseStoreEntity.CreatedAt.Between(utils.DateTimeStrToTime(req.StartDate), utils.DateTimeStrToTime(req.EndDate)))
	}
	if req.WarehouseID != "" {
		tx = tx.Where(dao.PurchaseStoreEntity.WarehouseID.Eq(cast.ToInt64(req.WarehouseID)))
	}
	if req.Status != "" {
		statusInt := cast.ToInt64(req.Status)
		tx = tx.Where(dao.PurchaseStoreEntity.Status.Eq(statusInt))
	}
	offset, limit := utils.DbPage(req.PageNumber, req.PageSize)
	var getPurchaseStorePageBoResp = make([]bo.GetPurchaseStorePageBoResp, 0)
	count, err := queryBuilder(tx).
		Order(dao.PurchaseStoreEntity.ID.Desc()).
		ScanByPage(&getPurchaseStorePageBoResp, offset, limit)
	return getPurchaseStorePageBoResp, count, err
}

func (s _purchaseStoreService) GetPurchaseStoreByIdApi(ctx context.Context, enterpriseID, id int64) (bo.GetPurchaseStorePageBoResp, error) {
	var resp = bo.GetPurchaseStorePageBoResp{}
	err := queryBuilder(dao.PurchaseStoreEntity.WithContext(ctx).WithContext(ctx).Where(
		dao.PurchaseStoreEntity.EnterpriseID.Eq(enterpriseID),
		dao.PurchaseStoreEntity.ID.Eq(id)),
	).Scan(&resp)
	return resp, err
}

// GetPurchaseStoreByPurchaseOrderIdApi 根据采购单查看是否已经创建过
func (s _purchaseStoreService) GetPurchaseStoreByPurchaseOrderIdApi(ctx context.Context, id int64) (*model.PurchaseStoreEntity, error) {
	return dao.PurchaseStoreEntity.WithContext(ctx).WithContext(ctx).Where(dao.PurchaseStoreEntity.PurchaseOrdersID.Eq(id)).First()
}

func (s _purchaseStoreService) ModifyStatusByIdApi(ctx context.Context, id, status int64) error {
	if _, err := dao.PurchaseStoreEntity.WithContext(ctx).Where(dao.PurchaseStoreEntity.ID.Eq(id)).UpdateSimple(
		dao.PurchaseStoreEntity.Status.Value(status),
	); err != nil {
		return err
	}
	return nil
}

func (s _purchaseStoreService) ModifyTotalCountByIdApi(ctx context.Context, tx *dao.QueryTx, id int64, count decimal.Decimal, operateId int64) error {
	if _, err := tx.PurchaseStoreEntity.WithContext(ctx).Where(dao.PurchaseStoreEntity.ID.Eq(id)).UpdateSimple(
		dao.PurchaseStoreEntity.StoreCount.Value(count),
		dao.PurchaseStoreEntity.OperateDate.Value(time.Now()),
		dao.PurchaseStoreEntity.OperateID.Value(operateId),
	); err != nil {
		return err
	}
	return nil
}

func queryBuilder(tx dao.IPurchaseStoreEntityDo) dao.IPurchaseStoreEntityDo {
	return tx.Select(
		dao.PurchaseStoreEntity.ALL,
		dao.EnterprisePersonEntity.Name.As("PurchaserName"),
		dao.SupplierEntity.Name.As("SupplierName"),
		dao.PurchaseOrdersEntity.OrderNo.As("PurchaseOrdersOrderNo"),
		dao.WarehouseEntity.Name.As("WarehouseName"),
	).
		LeftJoin(dao.PurchaseOrdersEntity, dao.PurchaseOrdersEntity.ID.EqCol(dao.PurchaseStoreEntity.PurchaseOrdersID)).
		LeftJoin(dao.EnterprisePersonEntity, dao.EnterprisePersonEntity.ID.EqCol(dao.PurchaseStoreEntity.PurchaserID)).
		LeftJoin(dao.SupplierEntity, dao.SupplierEntity.ID.EqCol(dao.PurchaseStoreEntity.SupplierID)).
		LeftJoin(dao.WarehouseEntity, dao.WarehouseEntity.ID.EqCol(dao.PurchaseStoreEntity.WarehouseID))
}

var PurchaseStoreService = new(_purchaseStoreService)
